package com.hongye.broker.controller;

import com.hongye.broker.service.GroupService;
import com.hongye.broker.util.MyFileload;
import com.hongye.common.entity.CustomerEntity;
import com.hongye.common.entity.GroupEntity;
import com.hongye.broker.vo.GroupCustomerVo;
import com.hongye.common.exception.ServiceException;
import com.hongye.common.response.ResponseResult;
import jodd.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("group")
public class GroupController {

    @Autowired
    private GroupService groupService;

    //团购列表接口
    @RequestMapping("/findAll")
    public ResponseResult findAll(){
        List<GroupEntity> list = groupService.findAll();
        Map<String, Object> map = new HashMap<>();
        map.put("list",list);

        return new ResponseResult(200,"查询成功团购列表",map);
    }

    ////团购详情列表接口
    @RequestMapping("/xiangqing")
    public ResponseResult xiangqing(String groupId){
        if (StringUtil.isEmpty(groupId)){
            throw new ServiceException("202","团购楼盘id不能为空");
        }
        //查询详情
        GroupCustomerVo groupCustomerVo  = groupService.xiangqing(Integer.valueOf(groupId));
        //查询团购用户
        List<CustomerEntity> list = groupService.tgyh(Integer.valueOf(groupId));
        groupCustomerVo.setList(list);
        Map<String, Object> map = new HashMap<>();
        map.put("list",groupCustomerVo);

        //http://localhost:8092/group/kaituan
        return new ResponseResult(200,"查询成功团购详情列表",map);

    }

    //删除团购用户
    @RequestMapping("/del")
    public ResponseResult del(String customerId){
        if (StringUtil.isEmpty(customerId)){
            throw new ServiceException("202","删除团购id不能为空");
        }
        try {

            groupService.del(Integer.valueOf(customerId));
            return new ResponseResult(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(201,"删除失败");
        }
    }

    //开团与关团改变状态
    @RequestMapping("update")
    public ResponseResult update(String groupId,String open_status){
        if (StringUtil.isEmpty(groupId)){
            throw new ServiceException("202","开团id不能为空");
        }
        if (StringUtil.isEmpty(open_status)){
            throw new ServiceException("202","开团状态不能为空");
        }
        try {

            groupService.update(Integer.valueOf(groupId),Integer.valueOf(open_status));
            return new ResponseResult(200,"成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(201,"失败");
        }
    }

    //删除团购用户
    @RequestMapping("/dele")
    public ResponseResult dele(String groupId){
        if (StringUtil.isEmpty(groupId)){
            throw new ServiceException("202","删除团购用户的ID不能为空");
        }
        try {

            groupService.dele(Integer.valueOf(groupId));
            return new ResponseResult(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(201,"删除失败");
        }
    }


    //图片上传

    @RequestMapping("/tpsc")
    public ResponseResult tpsc(MultipartFile file){
        String upload = MyFileload.upload(file);
        return new ResponseResult(200,upload);
    }


    //新建团购
    @RequestMapping("/insert")
    public ResponseResult insert(@RequestBody GroupEntity groupEntity){
        if (StringUtils.isEmpty(String.valueOf(groupEntity.getBrokerId()))){
            throw new ServiceException("202","经纪人id不能为空");
        }
        if (StringUtils.isEmpty(String.valueOf(groupEntity.getGroupImage()))){
            throw new ServiceException("202","封面图片不能为空");
        }
        if (StringUtils.isEmpty(String.valueOf(groupEntity.getGroupTitle()))){
            throw new ServiceException("202","团购标题不能为空");
        }
        if (StringUtils.isEmpty(String.valueOf(groupEntity.getGroupAddress()))){
            throw new ServiceException("202","团购地址不能为空");
        }
        if (StringUtils.isEmpty(String.valueOf(groupEntity.getStartTime()))){
            throw new ServiceException("202","团购开始时间不能为空");
        }
        if (StringUtils.isEmpty(String.valueOf(groupEntity.getEndTime()))){
            throw new ServiceException("202","团购结束时间不能为空");
        }
        if (StringUtils.isEmpty(String.valueOf(groupEntity.getGroupIntroduce()))){
            throw new ServiceException("202","团购介绍不能为空");
        }
        try {
            groupService.insert(groupEntity);
            return new ResponseResult(200,"新建成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(201,"新建失败");
        }
    }

    //开团
    @RequestMapping("kaituan")
    public ResponseResult kaituan(@RequestBody GroupEntity groupEntity){
        if (StringUtils.isEmpty(String.valueOf(groupEntity.getGatherAddress())) || groupEntity.getGatherAddress()==null){
            throw new ServiceException("202","集合地址不能为空");
        }
        if (StringUtils.isEmpty(String.valueOf(groupEntity.getCarNumeral()))  || groupEntity.getCarNumeral()==null){
            throw new ServiceException("202","车牌号不能为空");
        }
        if (StringUtils.isEmpty(String.valueOf(groupEntity.getGatherTime())) || groupEntity.getGatherTime()==null){
            throw new ServiceException("202","集合时间不能为空");
        }
        try {
            groupService.kaituan(groupEntity);
            return new ResponseResult(200,"开团成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(201,"开团失败");
        }
    }
}
